﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fulcrum.ViewModelGenerator
{
    public class PassThroughPropertySource
    {
        private PassThroughPropertySource(string name)
        {
            this.Name = name;
            this.Properties = new List<IViewModelPassThroughPropertyConfiguration>();
        }

        public string Name
        {
            get;
            private set;
        }

        public List<IViewModelPassThroughPropertyConfiguration> Properties
        {
            get;
            private set;
        }

        public static ICollection<PassThroughPropertySource> GetPassThroughPropertySources(IViewModelConfiguration config)
        {
            List<PassThroughPropertySource> sources = new List<PassThroughPropertySource>();

            foreach (var passThroughProp in config.PassThroughProperties)
            {
                var source = CreateGetSource(sources, passThroughProp);
                source.Properties.Add(passThroughProp);
            }

            return sources;
        }

        static PassThroughPropertySource CreateGetSource(List<PassThroughPropertySource> sources, IViewModelPassThroughPropertyConfiguration passThroughProp)
        {
            foreach(var source in sources)
            {
                if(String.Equals(passThroughProp.SourceObject, source.Name))
                {
                    return source;
                }
            }

            var newSource = new PassThroughPropertySource(passThroughProp.SourceObject);

            sources.Add(newSource);

            return newSource;
        }
    }
}
